package com.ruoyi.common.tools.excel.base;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.excel.ExcelResult;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

public class MyExcelResult<T> implements ExcelResult<T> {

	/**
	 * 数据对象list
	 */
	@Setter
	@Getter
	private List<T> list;

	/**
	 * 错误信息列表
	 */
	@Setter
	@Getter
	private List<String> errorList;

	/**
	 * 校验信息列表
	 */
	@Setter
	@Getter
	private List<String> validateList;

	public MyExcelResult() {
		this.list = new ArrayList<>();
		this.errorList = new ArrayList<>();
		this.validateList = new ArrayList<>();
	}

	public MyExcelResult(ExcelResult<T> excelResult) {
		this.list = excelResult.getList();
		this.errorList = excelResult.getErrorList();
	}

	/**
	 * 获取导入回执
	 *
	 * @return 导入回执
	 */
	@Override
	public String getAnalysis() {
		//导入**条，成功**条，失败**条
		int count = list.size();
		int errorCount = errorList.size() + validateList.size();
		int successCount = list.size() - errorCount;
		if (count == 0) {
			return "读取失败，未解析到数据";
		} else {
			if (errorCount == 0) {
				return StrUtil.format("导入{}条，成功 {} 条", count, successCount);
			} else if (errorList.size() == 0) {
				return StrUtil.format("导入{}条，成功 {} 条，失败 {} 条", count, successCount, errorCount);
			} else {
				return StrUtil.format("导入{}条，校功 {} 条，失败 {} 条", count, successCount, errorCount);
			}
		}
	}

	public boolean isSuccess() {
		int count = list.size();
		int errorCount = errorList.size() + validateList.size();
		return errorCount > 0 && count > 0 ? false : true;
	}
}
